﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Master;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using System.ServiceModel;

namespace BranchOfSansar
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region Цэстэй ажиллах

        private void DrawMenu()
        {
            using (MasterClient obj = new MasterClient(Globals.Token))
            {
                Permission[] menus = obj.MenuList(0);                   

                foreach (var row1 in menus.Where(w=>w.ParentId==0  && w.Level==1).OrderBy(o=>o.PerOrder))
                {
                    RibbonPage page = new RibbonPage();
                    page.Text = row1.Description;

                    foreach (var row2 in menus.Where(w=>w.ParentId==row1.PerId && w.Level==2).OrderBy(o=>o.PerOrder))
                    {
                        RibbonPageGroup group = new RibbonPageGroup();
                        group.Text = row2.Description;
                        page.Groups.Add(group);
                        foreach (var row3 in menus.Where(w=>w.ParentId==row2.PerId && w.Level==3).OrderBy(o=>o.PerOrder))
                        {
                            BarButtonItem item = new BarButtonItem();
                            item.Caption = row3.Description;
                            item.RibbonStyle = RibbonItemStyles.Large;
                            item.Glyph = ResourcesHandle.GetImage(row3.Icon);
                            item.Tag = row3.ClassName;
                            item.ItemClick += new ItemClickEventHandler(MenuClick);
                            group.ItemLinks.Add(item);
                        }
                    }
                    ribbon.Pages.Add(page);
                }
            }
        }
        
        private void MenuClick(object sender, ItemClickEventArgs e)
        {
            Type a = Type.GetType(e.Item.Tag.ToString());

            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType() == a)
                {
                    form.Activate();
                    return;
                }
            }

           Form frm;

            frm = (Form)Activator.CreateInstance(a);

            if (frm != null)
            {
                frm.MdiParent = this;
                //frm.Text = e.Item.Caption;
                frm.Show();
            }
        }
        #endregion 

        private void frmMain_Load(object sender, EventArgs e)
        {
            barUser.Caption = "Хэрэглэгч:" + Globals.UserName;
            barDate.Caption = "Огноо:" + Globals.GetDate().ToString("yyyy.MM.dd");

            DrawMenu();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            using (MasterClient mstr = new MasterClient(Globals.Token))
            {
                mstr.Logout(Globals.UserId);
            }
        }

        

        private void ribbon_Click(object sender, EventArgs e)
        {

        }
    }
}
